package com.foreknow.dao.impl;

import com.foreknow.dao.foodDao;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Food;

import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-14 10:42
 */
public class FoodDaoImpl extends BaseDAO implements foodDao {
    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql="select * from food";
        MappingFactory instance = MappingFactory.getInstance();
        EntityMapping map = instance.getMap(MappingFactory.FOOD_MAPPING);
        List<Object> query = jdbcTemplate.query(sql, map);



        return query;
    }

    @Override
    public boolean save(Food food) throws SQLException {
        String sql="insert into food (foodName,foodExplain,foodPrice,businessId) values(?,?,?,?)";
        int update = jdbcTemplate.update(sql, food.getFoodName(), food.getFoodExplain(), food.getFoodPrice(), food.getBussinessId());


        return update==1;
    }

    @Override
    public boolean delete(int id) throws SQLException {
        String sql="delete from food where foodId=?";
        int update = jdbcTemplate.update(sql, id);
        return update==1;
    }

    @Override
    public Object getQueryById(int id) throws SQLException {
        String sql="select * from food where foodId=?";
        EntityMapping map = mappingFactory.getMap(MappingFactory.FOOD_MAPPING);
        List<Object> query = jdbcTemplate.query(sql, map, id);
        Object o = query.get(0);
        return o;
    }

    @Override
    public boolean Updatedao(Food food) throws SQLException {
        String sql="update food set foodName=?,foodExplain=?,foodPrice=?,businessId=? where foodId=?";
        int update = jdbcTemplate.update(sql, food.getFoodName(), food.getFoodExplain(), food.getFoodPrice(), food.getBussinessId(), food.getFoodId());


        return update==1;
    }
}
